package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSMailAction;
import leeon.bbsbrower.models.MailObject;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class MailListView extends ViewPart {
	public static final String ID = "bbsbrower.view.maillist";

	private Table table = null;
				
	//信件数据
	static MailObject[] mails = null;
	
	//入口标志，0 新邮，1 所有邮
	private static int en = 0;
	
	//一页大小
	public static int size = 19;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		table = new Table(parent, SWT.READ_ONLY|SWT.BORDER|SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		TableColumn col1 = new TableColumn(table, SWT.NONE);
		col1.setText("序号");
		col1.setWidth(100);
		
		TableColumn col2 = new TableColumn(table, SWT.NONE);
		col2.setText("状态");
		col2.setWidth(100);
		
		TableColumn col3 = new TableColumn(table, SWT.NONE);
		col3.setText("发信人");
		col3.setWidth(200);
		
		TableColumn col4 = new TableColumn(table, SWT.NONE);
		col4.setText("标题");
		col4.setWidth(400);
		
		TableColumn col5 = new TableColumn(table, SWT.NONE);
		col5.setText("日期");
		col5.setWidth(200);
		
				
		//上下左右的事件
		table.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					actionToMail();					
					e.doit = false;
					
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {
					
					actionContent();
					e.doit = false;
				//上
				} else if (e.keyCode == SWT.ARROW_UP) {
					int dc = table.getSelectionIndex();
					//上翻页
					if (dc == 0) {
						actionPageUp();
						e.doit = false;
					}					
				//下
				} else if (e.keyCode == SWT.ARROW_DOWN) {
					int dc = table.getSelectionIndex();
					//下翻页
					if (dc == table.getItemCount() - 1) {
						actionPageDown();
						e.doit = false;
					}
					
				} else if (e.keyCode == SWT.PAGE_DOWN) {
					actionPageDown();
					e.doit = false;
				} else if (e.keyCode == SWT.PAGE_UP) {
					actionPageUp();
					e.doit = false;
				
				//回某一封信
				} else if (e.character == 'r') {
					actionReMail();
					e.doit = false;
				}
			
				
			}

			public void keyReleased(KeyEvent e) {}
			
		});
	}
	
	
	/**
	 * 回邮件主菜单
	 */
	private void actionToMail() {
		//跳转回到右键操作列表
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MailView mail = (MailView)page.showView(MailView.ID);
			if (MailListView.en == 1) mail.refreshData(1);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 进入某个邮件
	 */
	private void actionContent() {
		//根据文章列表看文章的action
		int m = table.getSelectionIndex();
		String url = mails[m].getURL();

		
		//跳转进邮件
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MailConView con = (MailConView)page.showView(MailConView.ID);
			con.refreshData(m, url);
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}
	
	//下翻
	private void actionPageDown() {
		MailObject mail = (MailObject)table.getItem(table.getItemCount()-1).getData();
		String start = String.valueOf(Integer.parseInt(mail.getNumber()) + 1);
		refreshData(MailListView.en, 0, start);
	}
	
	//上翻
	private void actionPageUp() {
		MailObject mail = (MailObject)table.getItem(0).getData();
		String start = String.valueOf(Integer.parseInt(mail.getNumber()) - MailListView.size);
		refreshData(MailListView.en, MailListView.size-1, start);
	}
	
	
	//回信
	private void actionReMail() {
		int m = table.getSelectionIndex();
		String id = mails[m].getSender();
		String title = mails[m].getTitle();
		
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			SendMailView mail = (SendMailView)page.showView(SendMailView.ID);
			mail.refreshData(1, id, title, null, m);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 刷新数据
	 * @param blocks	 
	 */
	public void refreshData(int en, int index, String start) {
		//如果是-1则不赋值
		if (en != -1) MailListView.en = en;
		
		try {
			//提取数据		
			if (en != -1) {
				//新邮
				if (en == 0) {
					MailListView.mails = BBSMailAction.getNewMails();			
				//所有邮
				} else {
					MailListView.mails = BBSMailAction.getAllMails(start);
				}
				
			}
		} catch (Exception e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			e.printStackTrace();
		}
		
		
				
		table.removeAll();
		if (MailListView.mails != null && MailListView.mails.length != 0) {
			for (int i = 0; i < MailListView.mails.length; i ++) {
				TableItem it = new TableItem(table, SWT.NONE);
				it.setText(new String[] {
						MailListView.mails[i].getNumber(), 
						MailListView.mails[i].getStatus(),
						MailListView.mails[i].getSender(),
						MailListView.mails[i].getTitle(),
						MailListView.mails[i].getDate()
						}
				);
				it.setData(MailListView.mails[i]);
			}
			//选中某个
			table.setSelection(index);
		} else {
			MessageDialog.openInformation(this.getViewSite().getShell(), "信件列表", (MailListView.en==0)?"没有新信":"没有信件");
		}
				
		ApplicationActionBarAdvisor.setStatusLine();
	}	
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		table.setFocus();
	}
}